package com.sskj.one.mine;

import android.arch.lifecycle.Observer;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.one.R;
import com.sskj.one.component.DaggerUserDataComponent;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/10
 */
public class JieHuiActivity extends BaseActivity<JieHuiPresenter> {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tv_alipay)
    TextView tvAlipay;
    @BindView(R.id.tv_bank)
    TextView tvBank;
    @BindView(R.id.tv_alipay_status)
    TextView tvAlipayStatus;
    @BindView(R.id.tv_bank_status)
    TextView tvBankStatus;
    @BindView(R.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_jie_hui;
    }

    @Override
    public JieHuiPresenter getPresenter() {
        return new JieHuiPresenter();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (userViewModel != null) {
            userViewModel.update();
        }
    }

    @Override
    public void initView() {
        tvTitle.setText("结汇方式");
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                if (isEmpty(userData.getAlipay_nums())) {
                    tvAlipayStatus.setText("未绑定");
                    tvAlipayStatus.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.color787));
                } else {
                    tvAlipayStatus.setText("已绑定");
                    tvAlipayStatus.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.theme));
                }
                if (isEmpty(userData.getBank_nums())) {
                    tvBankStatus.setText("未绑定");
                    tvBankStatus.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.color787));
                } else {
                    tvBankStatus.setText("已绑定");
                    tvBankStatus.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.theme));
                }
            } else {
                userData = null;
                tvAlipayStatus.setText("未绑定");
                tvAlipayStatus.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.color787));
                tvBankStatus.setText("未绑定");
                tvBankStatus.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.color787));
            }
        });
    }

    @Override
    public void initData() {
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            userViewModel.update();
            new Handler().postDelayed(() -> smartRefreshView.finishRefresh(), 1000);
        });
        ClickUtil.click(300, tvAlipay, () -> {
            AddAlipayActivity.start(JieHuiActivity.this);
        });
        ClickUtil.click(300, tvBank, () -> {
            AddBankActivity.start(JieHuiActivity.this);
        });

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, JieHuiActivity.class);
        context.startActivity(intent);
    }
}
